package com.ms.p2p.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ms.p2p.bean.PageBean;
import com.ms.p2p.controller.BaseController;
import com.ms.p2p.entity.content.Article;
import com.ms.p2p.entity.content.ArticleCategory;
import com.ms.p2p.provider.ArticleProvider;
import com.ms.p2p.provider.ArticleCategoryProvider;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * Created by xing_ on 2017/6/6.
 */
@Controller
@RequestMapping("/")
public class ContentController extends BaseController {

    @Reference
    private ArticleProvider articleProvider;
    @Reference
    private ArticleCategoryProvider artileTypeProvider;

//    @RequestMapping({"/article/aboutus"})
//    public String list() {
//
//        return "customer/article/aboutus";
//    }

    @RequestMapping({"/article/download"})
    public String download() {

        return "customer/article/download";
    }

    /**
     * 获取
     */
    @RequestMapping({"/article/list/{cate_name}"})
    public String list(@PathVariable String cate_name,
     @RequestParam(value = "currentPage", defaultValue = "1", required = false)
                             Integer currentPage, Model model) {




        PageBean page = new PageBean();
        page.setPageSize(8);
        page.setCurrentPage(currentPage);
        ArticleCategory category = artileTypeProvider.getByCateName(cate_name);

        List<ArticleCategory> types = artileTypeProvider.getlist();
        Integer type_id = category.getId();
        List<Article> list = articleProvider.findPager(type_id, "1", page);
        page.setTotalCount(articleProvider.findCount(type_id, "1"));
        model.addAttribute("types", types);
        model.addAttribute("type", cate_name);
        model.addAttribute("page", page);
        model.addAttribute("list", list);

                if(category.getType().equals("0")){
            model.addAttribute("category", category);
            return "customer/article/content";

        }


//        if(cate_name.equals("intro")){
//            return "customer/article/intro";
//        }
//        if(category.getType().equals("0")){
//            model.addAttribute("category", category);
//            return "customer/article/content";
//
//        }

        return "customer/article/list";
    }

    /**
     * 新闻公告详情
     */
    @RequestMapping({"/article/{id}"})
    public String toNewsDetail(@PathVariable String id, Model model) {
        List<ArticleCategory> types = artileTypeProvider.getlist();
        Article article = articleProvider.get(id);
        model.addAttribute("article", article);
        model.addAttribute("types", types);
        return "customer/article/detail";
    }





}
